package com.tpddns.flannery.flanneryscreenshots;


import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.ListView;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created by ZHXG on 2018/6/13.
 */

public class ZHXGService extends AccessibilityService {

    private static final String TAG = "ZHXGService";

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (nodeInfo != null) {
            String packageName = String.valueOf(event.getPackageName());
            if (packageName.equalsIgnoreCase("com.tencent.mm")) {
                List<AccessibilityNodeInfo> find = nodeInfo.findAccessibilityNodeInfosByText("发现");
                if (find.size() > 0 && find.get(0).getText().equals("发现")) {
                    find.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/list");
                if (list.size() > 0) {
                    list.get(0).getChild(1).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                    String mSavedPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "screenshot.png";
//                    try {
//                        Runtime.getRuntime().exec("screencap -p " + mSavedPath);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                List<AccessibilityNodeInfo> ln = nodeInfo.findAccessibilityNodeInfosByViewId("com.tencent.mm:id/ln");
//                if (ln.size() > 0) {
//                    ln.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                }

                    nodeInfo.recycle();

                    Intent i = new Intent("androidyuan.shotter");
                    // 这个不是必需的
                    i.addCategory(Intent.CATEGORY_DEFAULT);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
//                    View dView = MyApplication.mActivityInstance.getWindow().getDecorView();
//                    dView.setDrawingCacheEnabled(true);
//                    dView.buildDrawingCache();
//                    Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
//                    if (bitmap != null) {
//                        try {
//                            // 获取内置SD卡路径
//                            String sdCardPath = Environment.getExternalStorageDirectory().getPath();
//                            // 图片文件路径
//                            String filePath = sdCardPath + File.separator + "screenshot.png";
//                            File file = new File(filePath);
//                            FileOutputStream os = new FileOutputStream(file);
//                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
//                            os.flush();
//                            os.close();
//                            //DebugLog.d("a7888", "存储完成");
//                        } catch (Exception e) {
//                        }
//                    }

//                    View decorView = MyApplication.mActivityInstance.getWindow().getDecorView();
//                    Bitmap bitmapCompress = MainActivity.createBitmapSimpleView(decorView, MyApplication.mActivityInstance);
//
//                    //ListView listView = (ListView) MyApplication.mActivityInstance.getWindow().getDecorView().findViewById(R.id.dja);
//                    //ListView listView = findViewById(R.id.myListView);
//                    // 获取截屏
//                    //Bitmap bitmap = MainActivity.createBitmap(listView, MyApplication.mActivityInstance);
//                    // 压缩图片
//                    //Bitmap bitmapCompress = MainActivity.compressImage(bitmap);
//                    // 存储图片
//                    MainActivity.saveImageToGallery(MyApplication.mActivityInstance, bitmapCompress, "/images");


                }
            }
            if (packageName.equalsIgnoreCase("com.sina.weibo")) {
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("com.sina.weibo:id/rltitleSave");
                nodeInfo.recycle();
                if (list.size() > 0) {
                    list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        }
    }


    @Override
    public void onInterrupt() {

    }
}
